package com.jxbeidou.system.dict.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import com.jxbeidou.core.entity.PageResult;
import com.jxbeidou.core.mybatis.service.impl.BaseServiceImpl;
import com.jxbeidou.core.mybatis.convert.PageResultConvert;
import com.jxbeidou.system.dict.convert.DictDataConvert;
import com.jxbeidou.system.dict.entity.DictDataDO;
import com.jxbeidou.system.dict.query.DictDataQuery;
import com.jxbeidou.system.dict.vo.DictDataVO;
import com.jxbeidou.system.dict.mapper.DictDataMapper;
import com.jxbeidou.system.dict.service.DictDataService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 字典数据表
 *
 * @author 杜晓祥 
 * @since 1.0.0 2023-12-14
 */
@Service
@AllArgsConstructor
public class DictDataServiceImpl extends BaseServiceImpl<DictDataMapper, DictDataDO> implements DictDataService {

    @Override
    public PageResult<DictDataVO> page(DictDataQuery query) {
        IPage<DictDataDO> page = baseMapper.selectPage(getPage(query), getWrapper(query));
        PageResult<DictDataVO> result = PageResultConvert.convert(page, entityList -> DictDataConvert.INSTANCE.convertList(entityList));
        return result;
    }

    private LambdaQueryWrapper<DictDataDO> getWrapper(DictDataQuery query){
        LambdaQueryWrapper<DictDataDO> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(query.getDictTypeId() != null, DictDataDO::getDictTypeId, query.getDictTypeId());
        wrapper.like(StringUtils.isNotEmpty(query.getDictLabel()), DictDataDO::getDictLabel, query.getDictLabel());
        wrapper.eq(StringUtils.isNotEmpty(query.getLabelClass()), DictDataDO::getLabelClass, query.getLabelClass());
        return wrapper;
    }

    @Override
    public void save(DictDataVO vo) {
        DictDataDO entity = DictDataConvert.INSTANCE.convert(vo);

        baseMapper.insert(entity);
    }

    @Override
    public void update(DictDataVO vo) {
        DictDataDO entity = DictDataConvert.INSTANCE.convert(vo);

        updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Long> idList) {
        removeByIds(idList);
    }

}